<?php

class JasperReportController extends Controller
{
	public function actionReport1($id,$type=null)
	{
		
		switch($type){
			case 1:$reportName="report1";break;
			case 2:$reportName="report2";break;
			case 3:$reportName="report3";break;
			case 4:$reportName="report4";break;
		}
		$report=new JasperReport;
		$report->checkJavaExtension();
		$params = new Java("java.util.HashMap");
		$params->put("documentID",$id);
		$params->put("SUBREPORT_DIR", realpath(".")."/report");
		$connectionString=preg_replace('/mysql:host=(.*);dbname=(.*)/is',"jdbc:mysql://$1:3306/$2",Yii::app()->db->connectionString);
		$class = new JavaClass("java.lang.Class");
		$class->forName("com.mysql.jdbc.Driver");
		$driverManager = new JavaClass("java.sql.DriverManager");
		$conn = $driverManager->getConnection($connectionString,Yii::app()->db->username,Yii::app()->db->password);
		/* 		exporter.setParameter(JRDocxExporterParameter.OUTPUT_FILE, new File(outFileName));
		 exporter.exportReport();
		import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
		import net.sf.jasperreports.engine.export.ooxml.JRDocxExporterParameter; */
		//$conn=null;
		$file=$report->getReport($reportName,$params,$conn);
		//header("Content-type: application/pdf");
		header("Content-type: application/vnd.ms-word");
		header("Content-Disposition: attachment; filename=output.docx");
		readfile($file);
		unlink($file);
	
		//$this->render('index');
	}
	public function actionRequestReport($id)
	{
		$request=Request::model()->findByPk($id);
		switch($request->requestType){
			case 0 : {$reportName="report1";break;}
			case 1 : {$reportName="report2";break;}
			default : {exit;}
		}
		$report=new JasperReport;
		$report->checkJavaExtension();
		$params = new Java("java.util.HashMap");
		$params->put("requestID",$id);
		$params->put("dateText","วันที่");
		$params->put("monthText","เดือน");
		$params->put("officeText","สำนักงาน");
		$params->put("imagePath","http://".Yii::app()->request->getServerName()."/".Yii::app()->request->baseUrl."/attachments/");
		//$locale = new java("java.util.Locale","th", "TH");
		//$params->put("REPORT_LOCALE", $locale);
		$connectionString=eregi_replace("mysql:host=(.*);dbname=(.*)","jdbc:mysql://\\1:3306/\\2",Yii::app()->db->connectionString);
		$class = new JavaClass("java.lang.Class");
		$class->forName("com.mysql.jdbc.Driver");
		$driverManager = new JavaClass("java.sql.DriverManager");
		$conn = $driverManager->getConnection($connectionString,Yii::app()->db->username,Yii::app()->db->password);
		$file=$report->getReport($reportName,$params,$conn);
		header("Content-type: application/pdf");
		readfile($file);
		unlink($file);
		
		//$this->render('index');
	}
	
	public function actionProductValueReport()
	{
		$report=new JasperReport;
		$report->checkJavaExtension();
		$params = new Java("java.util.HashMap");
		//$locale = new java("java.util.Locale","th", "TH");
		//$params->put("REPORT_LOCALE", $locale);
		$connectionString=eregi_replace("mysql:host=(.*);dbname=(.*)","jdbc:mysql://\\1:3306/\\2",Yii::app()->db->connectionString);
		$class = new JavaClass("java.lang.Class");
		$class->forName("com.mysql.jdbc.Driver");
		$driverManager = new JavaClass("java.sql.DriverManager");
		$conn = $driverManager->getConnection($connectionString,Yii::app()->db->username,Yii::app()->db->password);
		$file=$report->getReport("report3",$params,$conn);
		header("Content-type: application/pdf");
		readfile($file);
		unlink($file);
		
		//$this->render('index');
	}
public function actionProductReport($id)
	{
		$report=new JasperReport;
		$report->checkJavaExtension();
		$params = new Java("java.util.HashMap");
		$params->put("productID",$id);
		$connectionString=eregi_replace("mysql:host=(.*);dbname=(.*)","jdbc:mysql://\\1:3306/\\2",Yii::app()->db->connectionString);
		$class = new JavaClass("java.lang.Class");
		$class->forName("com.mysql.jdbc.Driver");
		$driverManager = new JavaClass("java.sql.DriverManager");
		$conn = $driverManager->getConnection($connectionString,Yii::app()->db->username,Yii::app()->db->password);
		$file=$report->getReport("report4",$params,$conn);
		header("Content-type: application/pdf");
		readfile($file);
		unlink($file);
		
		//$this->render('index');
	}
	public function actionRequestProductReport()
	{
		$report=new JasperReport;
		$report->checkJavaExtension();
		$params = new Java("java.util.HashMap");
		//$locale = new java("java.util.Locale","th", "TH");
		//$params->put("REPORT_LOCALE", $locale);
		$connectionString=eregi_replace("mysql:host=(.*);dbname=(.*)","jdbc:mysql://\\1:3306/\\2",Yii::app()->db->connectionString);
		$class = new JavaClass("java.lang.Class");
		$class->forName("com.mysql.jdbc.Driver");
		$driverManager = new JavaClass("java.sql.DriverManager");
		$conn = $driverManager->getConnection($connectionString,Yii::app()->db->username,Yii::app()->db->password);
		$file=$report->getReport("report5",$params,$conn);
		header("Content-type: application/pdf");
		readfile($file);
		unlink($file);
		
		//$this->render('index');
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}